package org.dromara.module.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.WxMessage;
import org.dromara.module.domain.Parameter;
import org.dromara.module.mapper.ParameterMapper;
import org.dromara.module.service.ParameterService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@RequiredArgsConstructor
@Service
public class ParameterServiceImpl extends ServiceImpl<ParameterMapper, Parameter> implements ParameterService {


    @Resource
    private ParameterMapper parameterMapper;
    @Resource
    protected RedisTemplate<String, Object> redisTemplate;


    @Value("${wx.appid}")
    private String appid;
    @Value("${wx.secret}")
    private String secret;


    @Override
    public Parameter getParameterById(int id) {
        Parameter parameter = null;
        String key = "parameter_" + id;
        boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
        if (blHasKey) {
            Object object = redisTemplate.opsForHash().get(key, key);
            String itemDicts = object != null ? object.toString() : "";
            if (StrUtil.isNotEmpty(itemDicts)) {
                JSONObject jsonObject = JSONUtil.parseObj(itemDicts);
                parameter = JSONUtil.toBean(JSONUtil.toJsonStr(jsonObject), Parameter.class);
            } else {
                parameter = parameterMapper.selectOneByColumn("is_default",1);
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(parameter));
            }
        } else {
            parameter = parameterMapper.selectOneByColumn("is_default",1);
            redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(parameter));
        }
        return parameter;
    }



    @Override
    @Transactional
    public String getAccessToken(int type) {
        String access_token = "";
        try{
            Parameter parameter = parameterMapper.selectById(1);
            if(type == 1){//小程序
                access_token = parameter.getAccessToken();
                if(new Date().getTime() > parameter.getAcLastTime().getTime()) {
                    access_token = WxMessage.getACCESSTOKEN(appid, secret);
                    parameter.setAccessToken(access_token);
                    parameter.setAcLastTime(DateUtils.getAfterSecondsByOld(new Date(),7200));
                    parameterMapper.updateById(parameter);
                }
            }
            resetParameter(1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return access_token;
    }


    @Override
    public void resetParameter(int id) {
        String key = "parameter_" + id;
        Parameter parameter = parameterMapper.selectOneByColumn("is_default",1);
        redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(parameter));
    }


}
